package hangMan;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileSystemView;

public class Words extends JPanel {

	private JLabel wordInPanel = new JLabel("",SwingConstants.CENTER);
	private String correctWordString;
	private StringBuilder wordToGuess = new StringBuilder();
	private HangManDraw painter;
	private OtherButtons otherButtons;
	private Font labelFont = new Font("wordInLabel", Font.BOLD, 25);
	private AlphabetPane alphabet;
	private Random random;
	private int randomInt;
	private FileRead FilesToRead;
	private String categoryLabel;
	private Word word;
	private String labelvalue;

	public Words() {
		setLayout(new BorderLayout());
		FilesToRead = new FileRead();
		alphabet = new AlphabetPane();
		otherButtons = new OtherButtons();
		painter = new HangManDraw();
		play();

		add(BorderLayout.CENTER, painter);
		add(BorderLayout.WEST, otherButtons);
		add(BorderLayout.SOUTH, alphabet);
		add(BorderLayout.NORTH, wordInPanel);

	}
	
	public void play(){
		getWordFromList(FilesToRead.getLists());
		labelvalue = "Guess the "+word.getType()+": ";
		wordInPanel.setText(labelvalue + setWordInPanel() );
		wordInPanel.setFont(labelFont);
	}

	public StringBuilder getWordToGuess() {
		return wordToGuess;
	}

	public void getWordFromList(ArrayList<Word> l) {
		random = new Random();
		randomInt = random.nextInt(l.size());
		word = l.get(randomInt);
		correctWordString = new String(word.getName());
	}
	


	public void checkforWin() {
		if (wordToGuess.toString().equalsIgnoreCase(correctWordString)) {
			painter.setErrors(-1);
			painter.repaint();
			disableButtons();
		}

	}

	public void disableButtons() {
		for (int i = 0; i < alphabet.getComponentCount(); i++) {
			alphabet.getComponent(i).setEnabled(false);
		}
	}
	
	public void enebleButtons() {
		for (int i = 0; i < alphabet.getComponentCount(); i++) {
			alphabet.getComponent(i).setEnabled(true);
		}
	}

	public String setWordInPanel() {
		for (int i = 0; i < correctWordString.length(); i++) {
			wordToGuess.append('*');
		}
		return wordToGuess.toString();

	}

	public void guess(String l) {
		boolean correct = false;
		for (int i = 0; i < correctWordString.length(); i++) {
			char right = correctWordString.charAt(i);
			char guessed = l.charAt(0);
			if (right == guessed) {
				wordToGuess.setCharAt(i, guessed);
				wordInPanel.setText(labelvalue + wordToGuess.toString());
				correct = true;
			}

		}

		if (!correct) {
			painter.refreshErrors();
			painter.repaint();
		}

	}

	private class AlphabetPane extends JPanel {

		private JButton button;

		public AlphabetPane() {
			setLayout(new GridLayout(2, 13));
			createButtons();
		}

		private void createButtons() {
			AlphabetListner letter = new AlphabetListner();
			StringBuilder buttonText = new StringBuilder();
			char ch = 'a';
			for (int i = 0; i < 26; i++) {
				buttonText.append(ch);
				button = new JButton(buttonText.toString());
				button.addActionListener(letter);
				add(button);
				ch += 1;
				buttonText.setLength(0);
			}
		}

		private class AlphabetListner implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				JButton letterButton = (JButton) e.getSource();
				String letter = letterButton.getText();
				guess(letter);
				letterButton.setEnabled(false);
				checkforWin();
				if (painter.getErrors() >= 9) {
					disableButtons();
				}
			}

		}

	}

	private class OtherButtons extends JPanel {

		private JButton restartButton;
		private JButton infoButton;

		public OtherButtons() {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			String[] categories = {"Animals", "Countries", "Cities" };

			restartButton = new JButton("Restart");
			add(restartButton);
			add(Box.createRigidArea(new Dimension(0, 500)));
			RestartListner resListner = new RestartListner();
			restartButton.addActionListener(resListner);
		
		}
		
		private class RestartListner implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				painter.setErrors(0);
				wordToGuess.setLength(0);
				play();
				painter.repaint();
				enebleButtons();
				
				
			}
			
		}
		
		

	}

}
